package com.youkeyi.ddy.cloud.basic.pub.mapping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youkeyi.ddy.cloud.basic.pub.mapping.entity.SysMapping;
import com.youkeyi.ddy.cloud.basic.pub.mapping.mapper.SysMappingDao;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessDeleteParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.param.BusinessQueryParam;
import com.youkeyi.ddy.cloud.basic.pub.mapping.service.SysMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 映射关系表 服务实现类
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-02-19
 */
@Slf4j
@Service
public class SysMappingServiceImpl extends ServiceImpl<SysMappingDao, SysMapping> implements SysMappingService {

    @Autowired
    private SysMappingDao sysMappingDao;

    @Override
    public List<SysMapping> businessQuery(BusinessQueryParam param) {
        if (null == param) {
            return new ArrayList<>();
        }
        if (StringUtils.isBlank(param.getMappingType()) || StringUtils.isBlank(param.getKeyType())
                || CollectionUtils.isEmpty(param.getKeys())) {
            return new ArrayList<>();
        }

        QueryWrapper<SysMapping> wrapper = new QueryWrapper<>();
        if (YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value().equals(param.getKeyType())) {
            wrapper.lambda()
                    .in(SysMapping::getSourceKey, param.getKeys());
        } else if (YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_TARGET.value().equals(param.getKeyType())) {
            wrapper.lambda()
                    .in(SysMapping::getTargetKey, param.getKeys());
        } else {
            log.error("入参Key Type参数不正确，无法查询");
            return new ArrayList<>();
        }
        wrapper.lambda()
                .eq(SysMapping::getMappingType, param.getMappingType());

        return this.list(wrapper);
    }

    @Override
    public Boolean businessCreate(List<SysMapping> param) {
        if (CollectionUtils.isEmpty(param)) {
            return Boolean.FALSE;
        }
        for (SysMapping item : param) {
            if (StringUtils.isBlank(item.getMappingType())
                    || StringUtils.isBlank(item.getSourceKey()) || StringUtils.isBlank(item.getTargetKey())) {
                log.error("入参存在参数为空的情况，无法插入");
                return Boolean.FALSE;
            }
            if (StringUtils.isBlank(item.getPrimaryKey())) {
                item.setPrimaryKey(SimpleUUidUtils.getUuid());
            }
        }
        return this.saveBatch(param);
    }

    @Override
    public Boolean businessDelete(BusinessDeleteParam param) {
        if (null == param) {
            return Boolean.FALSE;
        }
        if (StringUtils.isBlank(param.getMappingType()) || StringUtils.isBlank(param.getKeyType())
                || CollectionUtils.isEmpty(param.getKeys())) {
            return Boolean.FALSE;
        }

        BusinessQueryParam queryParam = new BusinessQueryParam();
        queryParam.setMappingType(param.getMappingType());
        queryParam.setKeys(param.getKeys());
        queryParam.setKeyType(param.getKeyType());
        List<SysMapping> sysMappings = this.businessQuery(queryParam);
        if (CollectionUtils.isEmpty(sysMappings)) {
            return Boolean.TRUE;
        }
        return this.removeByIds(sysMappings.stream().map(SysMapping::getId).collect(Collectors.toList()));
    }

    @Override
    public Boolean businessDelete(List<SysMapping> mappings) {
        if (CollectionUtils.isEmpty(mappings)) {
            return Boolean.FALSE;
        }
        return sysMappingDao.businessDelete(mappings);
    }
}
